<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Home extends MY_Controller
{  
    public function index()
    {
        $this->load->model("repos/symbolmodel", "SymbolModel");
        $data['arrSymbol'] = $this->SymbolModel->getSymbol();
        $data['isLoadCalendar'] = true;
        $this->renderView('home/index', $data);
    }

    public function getcomparedata()
    {
        if (!$this->input->post()) {
            show_404();
        }
        $getResult = $this->input->post("getresult", true);
        if ($getResult == 1) {
            //check date valid
            $startDate = trim($this->input->post("txtstartdate", true));
            $endDate = trim($this->input->post("txtenddate", true));
            if($startDate==''){
                $dateError = "<span class='error'><strong>Start date</strong> was not valid !</span>";
                $this->load->view('common/script', array('message' => $dateError));
                return;
            }
            if($endDate==''){
                $dateError = "<span class='error'><strong>End date</strong> was not valid !</span>";
                $this->load->view('common/script', array('message' => $dateError));
                return;
            }
            try {
                if (!$this->checkDate($startDate)) {
                    $dateError = "<span class='error'><strong>Start date</strong> was not valid !</span>";
                    $this->load->view('common/script', array('message' => $dateError));
                    return;
                }
                if (!$this->checkDate($endDate)) {
                    $dateError = "<span class='error'><strong>End date</strong> was not valid !</span>";
                    $this->load->view('common/script', array('message' => $dateError));
                    return;
                }

                $startDate = new DateTime($startDate, new DateTimeZone(TIMEZONE));
                $endDate = new DateTime($endDate, new DateTimeZone(TIMEZONE));
                if ($endDate <= $startDate) {
                    throw new Exception('E_0001');
                }
            }
            catch (exception $ex) {
                $dateError = "";
                if ($ex->getMessage() == 'E_0001') {
                    $dateError = "<span class='error'><strong>Start date</strong> must be less than <strong>end date</strong> !</span>";
                } else {
                    $dateError = "<span class='error'><strong>Start date</strong> or <strong>end date</strong> was not valid !</span>";
                }
                $this->load->view('common/script', array('message' => $dateError));
                return;
            }

            //check company valid
            $cmpA = $this->input->post("cbxcmpa", true);
            $cmpB = $this->input->post("cbxcmpb", true);
            if ($cmpA != $cmpB) {
                $this->load->model("repos/symbolmodel", "SymbolModel");
                $arrSymbol = $this->SymbolModel->getSymbol();
                $data['companyA'] = $this->getUrlValFromId($arrSymbol, $cmpA);
                $data['companyB'] = $this->getUrlValFromId($arrSymbol, $cmpB);
                $arrCmpA = $this->SymbolModel->getSymbolData($startDate, $endDate, $cmpA);
                $arrCmpB = $this->SymbolModel->getSymbolData($startDate, $endDate, $cmpB);
                if (empty($arrCmpA) || empty($arrCmpB)) {
                    $dateError = "<span class='error'>" . NO_RECORD_FOUND . " for <strong>{$data['companyA']}</strong> and <strong>{$data['companyB']}</strong> from <strong>{$startDate->format('d-M-Y')}</strong> to <strong>{$endDate->format('d-M-Y')}</strong> !</span>";
                    $this->load->view('common/script', array('message' => $dateError));
                } else {
                	$this->load->library('calculatordata');
                    $data['arrCmp'] = $this->calculatordata->getDataOutPut($arrCmpA, $arrCmpB, $data['companyA'], $data['companyB']);

                    //check return data
                    if (count($data['arrCmp']) > 0) {
                        $this->load->view('home/table', $data);
                    } else {
                        $dateError = "<span class='error'> " . NO_RECORD_FOUND . " !</span>";
                        $this->load->view('common/script', array('message' => $dateError));
                    }
                }
            } else {
                $dateError = "<span class='error'><strong>Banks</strong> were the same !</span>";
                $this->load->view('common/script', array('message' => $dateError));
            }
            return;
        } else {
            show_404();
        }
    }
}
